package com.hnjmxy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.hnjmxy.pojo.Customer;
import com.hnjmxy.utils.DataConn;

public class CustomerDao {   //锟斤拷装锟角讹拷Customer锟斤拷锟斤拷械牟锟斤拷搿⑸撅拷锟斤拷锟斤拷薷摹锟斤拷锟窖拷锟斤拷锟�
	
	//锟斤拷装锟斤拷锟斤拷锟斤拷锟�---->锟斤拷锟斤拷锟斤拷锟斤拷锟诫、头锟今、电话......-->锟斤拷锟斤拷锟角凤拷装Customer锟斤拷锟斤拷锟斤拷
	//锟斤拷锟诫、删锟斤拷锟斤拷锟睫革拷  --->excuteUpdate()-->1 or 0
	//锟斤拷询锟斤拷锟斤拷---->excuteQuery()
	public int regCustomer(Customer c){
		int flag = 0;
		//1锟斤拷锟斤拷锟斤拷锟捷匡拷
		Connection conn = DataConn.getConn();
		//2锟斤拷写SQL锟斤拷锟�
		String sql = "insert into customer(c_name,c_pass) values(?,?)";  //?锟斤拷占位锟斤拷锟斤拷预锟斤拷锟斤拷SQL
		//String sql = "insert into customer values(?,?,?,?,?,?,?,?,?,?)";  //?锟斤拷占位锟斤拷锟斤拷预锟斤拷锟斤拷SQL
		//String sql = "insert into customer values(?,?,'','','','','','','','')";
		try {//3锟斤拷锟斤拷PreparedStatement锟斤拷锟斤拷
			PreparedStatement pst = conn.prepareStatement(sql);
			//4锟斤拷?锟脚革拷值
			pst.setString(1, c.getC_name());   //注锟解，锟斤拷锟斤拷侄锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷荩锟絪etInt()
		//	pst.setString(2, MD5Util.stringToMD5(c.getC_pass()));
			pst.setString(2,c.getC_pass());
//			pst.setString(3, c.getC_header());
//			pst.setString(4, c.getC_phone());
//			pst.setString(5, c.getC_question());
//			pst.setString(6, c.getC_answer());
//			pst.setString(7, c.getC_address());
			//1String-->java.util.Date-->java.sql.Date
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			try {
//				java.util.Date d = sdf.parse(c.getC_birthday());
//				pst.setDate(8, new java.sql.Date(d.getTime()));//setDate(int,java.sql.Date)
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			//2注锟斤拷锟斤拷锟节猴拷时锟斤拷----锟斤拷锟斤拷取锟斤拷前系统锟斤拷锟节猴拷时锟斤拷
//			java.util.Date regtime = new java.util.Date();
//			pst.setDate(9, new java.sql.Date(regtime.getTime()));
//			
//			pst.setString(10, c.getC_email());
//			System.out.println("锟斤拷锟斤拷....."+c.getC_birthday()+"");
			flag = pst.executeUpdate();  //5执锟斤拷SQL锟斤拷锟�
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}
	public int regCustomer1(Customer c){
		int flag = 0;
		//1锟斤拷锟斤拷锟斤拷锟捷匡拷
		Connection conn = DataConn.getConn();
		//2锟斤拷写SQL锟斤拷锟�
		String sql = "insert into customer(c_name,c_pass) values(?,?)";  //?锟斤拷占位锟斤拷锟斤拷预锟斤拷锟斤拷SQL
		//String sql = "insert into customer values(?,?,?,?,?,?,?,?,?,?)";  //?锟斤拷占位锟斤拷锟斤拷预锟斤拷锟斤拷SQL
		//String sql = "insert into customer values(?,?,'','','','','','','','')";
		try {//3锟斤拷锟斤拷PreparedStatement锟斤拷锟斤拷
			PreparedStatement pst = conn.prepareStatement(sql);
			//4锟斤拷?锟脚革拷值
			pst.setString(1, c.getC_name());   //注锟解，锟斤拷锟斤拷侄锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷荩锟絪etInt()
		//	pst.setString(2, MD5Util.stringToMD5(c.getC_pass()));
			pst.setString(2,c.getC_pass());
//			pst.setString(3, c.getC_header());
//			pst.setString(4, c.getC_phone());
//			pst.setString(5, c.getC_question());
//			pst.setString(6, c.getC_answer());
//			pst.setString(7, c.getC_address());
			//1String-->java.util.Date-->java.sql.Date
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			try {
//				java.util.Date d = sdf.parse(c.getC_birthday());
//				pst.setDate(8, new java.sql.Date(d.getTime()));//setDate(int,java.sql.Date)
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			//2注锟斤拷锟斤拷锟节猴拷时锟斤拷----锟斤拷锟斤拷取锟斤拷前系统锟斤拷锟节猴拷时锟斤拷
//			java.util.Date regtime = new java.util.Date();
//			pst.setDate(9, new java.sql.Date(regtime.getTime()));
//			
//			pst.setString(10, c.getC_email());
//			System.out.println("锟斤拷锟斤拷....."+c.getC_birthday()+"");
			flag = pst.executeUpdate();  //5执锟斤拷SQL锟斤拷锟�
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}
	//锟斤拷装锟斤拷示customer锟斤拷锟斤拷锟斤拷;锟捷的凤拷锟斤拷-->一锟斤拷锟斤拷锟斤拷-->锟斤拷锟斤拷锟斤拷(锟斤拷锟斤拷)
	public List<Customer>   showCustomer(){
		List<Customer> list = new ArrayList<Customer>();
		//1锟斤拷锟斤拷锟斤拷锟捷匡拷
		Connection conn = DataConn.getConn();
		//2 锟斤拷写SQL锟斤拷锟�
		String sql = "select * from customer";  //锟斤拷锟斤拷锟斤拷锟斤拷锟侥诧拷询锟斤拷锟斤拷锟斤拷sql锟斤拷锟�
		try {//3锟斤拷锟斤拷Preparedstatment锟斤拷锟斤拷
			PreparedStatement pst = conn.prepareStatement(sql);
			//4锟斤拷?锟斤拷值---->省锟斤拷
			//5执锟斤拷SQL锟斤拷锟�
			ResultSet rs = pst.executeQuery();  //锟斤拷锟斤拷锟铰硷拷锟�-->锟斤拷锟斤拷
			//6 rs--->list
			Customer c; //锟斤拷锟斤拷一锟斤拷c锟斤拷锟斤拷
			while(rs.next()) {
				//锟斤拷rs锟斤拷锟揭伙拷锟斤拷锟斤拷莘锟阶帮拷锟絚ustomer锟斤拷锟斤拷锟斤拷
				c = new Customer();
				c.setC_name(rs.getString("c_name"));//锟斤拷c.setC_name(rs.getString(0))
				c.setC_pass(rs.getString(1));
				c.setC_phone(rs.getString(2));
				c.setC_birthday(rs.getString(8));  //getInt,getFloat,getDouble,getDate()
			      list.add(c);  //锟斤拷c锟斤拷拥锟斤拷锟斤拷锟斤拷锟�    锟斤拷千锟斤拷要锟斤拷锟斤拷
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	//删锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷
	public int delCustomer(String c_name){
		int flag = 0;
		Connection conn = DataConn.getConn();
		String sql = "delete from customer where c_name=?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, c_name);
			flag = ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
<<<<<<< HEAD
	
	public int delCustomer1(String c_name){
		int flag = 0;
		Connection conn = DataConn.getConn();
		String sql = "delete from customer where c_name=?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, c_name);
			flag = ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	//根据用户名获取用户信息
=======
	//锟斤拷锟斤拷锟矫伙拷锟斤拷锟斤拷取锟矫伙拷锟斤拷息
>>>>>>> branch 'master' of https://gitee.com/weiyanming/testdemo.git
	public Customer  GetCustomer(String name){
		Customer cu = null;
		Connection conn = DataConn.getConn();//1锟斤拷锟斤拷锟斤拷锟捷匡拷
		//2锟斤拷写SQL锟斤拷锟�
		String sql = "select * from customer where c_name=?";
		try {	//3锟斤拷锟斤拷PreparedStatement锟斤拷锟斤拷,锟斤拷?锟脚革拷值
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			//4执锟斤拷SQL
			ResultSet rs = pst.executeQuery();    //只锟斤拷一锟斤拷锟斤拷锟斤拷
			if(rs.next()) {			//5锟斤拷锟斤拷执锟叫斤拷锟�
				cu = new Customer();
				cu.setC_name(rs.getString("c_name"));//锟斤拷c.setC_name(rs.getString(0))
				cu.setC_pass(rs.getString(1));
				cu.setC_phone(rs.getString(2));
				cu.setC_birthday(rs.getString(8));  
				
			}} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		return cu;
	}
	
	
	public int updatepwd(Customer c) {
		int flag = 0;
		Connection conn = DataConn.getConn();//1锟斤拷锟斤拷锟斤拷锟捷匡拷
	String sql = "update customer set c_pass=? where c_name=?";
	try {
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,c.getC_pass());
		ps.setString(2,c.getC_name());	
		flag = ps.executeUpdate();		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return flag;	
	}
	public int updateCustomer(Customer c) {
		int flag = 0;
		Connection conn = DataConn.getConn();//1锟斤拷锟斤拷锟斤拷锟捷匡拷
	String sql = "update customer set c_pass=?,c_phone=?,c_birthday=? where c_name=?";
	try {
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1,c.getC_pass());
		ps.setString(2,c.getC_phone());
		ps.setString(3,c.getC_birthday());
		ps.setString(4,c.getC_name());	
		flag = ps.executeUpdate();		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return flag;	
	}
	
	//锟矫伙拷锟斤拷录锟斤拷证---锟斤拷锟斤拷锟斤拷锟侥诧拷询
	//public ****  checkLogin(String name,String pwd){ 锟斤拷锟斤拷锟斤拷锟斤拷写锟斤拷锟斤拷一锟姐不锟斤拷锟斤拷锟斤拷
	public Customer  checkLogin(Customer c){
		Customer cu = null;
		Connection conn = DataConn.getConn();//1锟斤拷锟斤拷锟斤拷锟捷匡拷
		//2锟斤拷写SQL锟斤拷锟�
		String sql = "select * from customer where c_name=? and c_pass=?";
		try {	//3锟斤拷锟斤拷PreparedStatement锟斤拷锟斤拷,锟斤拷?锟脚革拷值
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, c.getC_name());
			pst.setString(2, c.getC_pass());
			//4执锟斤拷SQL
			ResultSet rs = pst.executeQuery();    //只锟斤拷一锟斤拷锟斤拷锟斤拷
			if(rs.next()) {			//5锟斤拷锟斤拷执锟叫斤拷锟�
				cu = new Customer();
				cu.setC_name(rs.getString("c_name"));//锟斤拷c.setC_name(rs.getString(0))
				cu.setC_pass(rs.getString(1));
				cu.setC_phone(rs.getString(2));
				cu.setC_birthday(rs.getString(8));  
			}} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		return cu;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
